package com.erdos.hyt.mall.service.impl;/**
 * @auth Liurong
 * @date 2017/4/1.
 */

import com.erdos.hyt.base.exception.ExceptionCode;
import com.erdos.hyt.base.exception.business.MallApiException;
import com.erdos.hyt.base.utils.CommonTools;
import com.erdos.hyt.mall.mapper.*;
import com.erdos.hyt.mall.model.*;
import com.erdos.hyt.mall.model.vo.MallOrderDetailVo;
import com.erdos.hyt.mall.service.MallConsigneeAddressService;
import com.erdos.hyt.mall.service.MallOrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 订单详情service实现类
 *
 * @author liurong
 * @create 2017/4/1
 **/
@Service("mallOrderDetailService")
public class MallOrderDetailServiceImpl implements MallOrderDetailService {

    @Autowired
    private MallGoodsMapper mallGoodsMapper;

    @Autowired
    private MallGoodsSkuMapper mallGoodsSkuMapper;

    @Autowired
    private MallGoodsTypeMapper mallGoodsTypeMapper;

    @Autowired
    private MallConsigneeAddressService mallConsigneeAddressService;

    @Autowired
    private MallOrderDetailMapper mallOrderDetailMapper;

    @Autowired
    private MallConsigneeAddressMapper mallConsigneeAddressMapper;

    @Override
    @Transactional
    public MallOrderDetail generateBaseOrder(Integer userId,Integer skuId, Integer num) {
       MallGoodsSku mallGoodsSku= mallGoodsSkuMapper.getMallGoodsSkuById(skuId);
       if(mallGoodsSku==null){
           throw new MallApiException(ExceptionCode.MALL_GOODS_INFO_WRONG);//商品信息错误
       }
       MallGoods mallGoods= mallGoodsMapper.selectMallGoodsById(mallGoodsSku.getGoodsId());
        if (mallGoods == null) {
            throw new MallApiException(ExceptionCode.MALL_GOODS_INFO_WRONG);//商品信息错误
        }
       MallGoodsType mallGoodsType= mallGoodsTypeMapper.getMallGoosdTypeById(mallGoods.getGoodsType());
        //组织订单展示数据
        MallConsigneeAddress mallConsigneeAddress=mallConsigneeAddressService.getLatelyAddress(userId);

        MallOrderDetail orderDetail=new MallOrderDetail();
        if(mallConsigneeAddress!=null){
            //封装地址信息
            orderDetail.setAddress(mallConsigneeAddress.getAddress());
            orderDetail.setReceivePostcode(mallConsigneeAddress.getPostNum());
            orderDetail.setReceiver(mallConsigneeAddress.getConsignee());
            orderDetail.setReceiverMobile(mallConsigneeAddress.getConsigneeMobile());
        }
        //封装产品信息
        orderDetail.setGoodName(mallGoods.getGoodsName());//商品名称
        orderDetail.setGoodCategory(mallGoodsSku.getCategoryName());//商品分类快照
        orderDetail.setGoodsId(mallGoods.getGoodsId());//商品ID
        orderDetail.setGoodType(mallGoodsType.getName());//商品类型快照
        orderDetail.setExpCompany("顺丰速运");//快递公司
        BigDecimal goodsTotalPrice=mallGoodsSku.getSalePrice().multiply(new BigDecimal(num));//商品总价
        //TODO 计算运费
        BigDecimal carriage=new BigDecimal(10);
        orderDetail.setPrice(mallGoodsSku.getSalePrice());//单价
        orderDetail.setTotalPrice(goodsTotalPrice.add(carriage));//总价=运费+商品总价
        orderDetail.setCarriage(carriage);
        orderDetail.setTradeNum(num);
        orderDetail.setOrderStatus("0");//订单状态为待提交
        orderDetail.setBillingUnit(mallGoodsSku.getUnit());//计量单位
        orderDetail.setWeight(mallGoodsSku.getWeight());
        orderDetail.setTotalWeight(mallGoodsSku.getWeight() * num);
        orderDetail.setSpecification(mallGoodsSku.getSpecification());
        orderDetail.setUserId(userId);//用户ID
        Date now=new Date();
        orderDetail.setOrderCode("hytMall_"+now.getTime());
        mallOrderDetailMapper.insertMallOrderDetail(orderDetail);
        return orderDetail;
    }

    @Override
    public MallOrderDetail updSubmitOrder(Integer addressId, MallOrderDetailVo mallOrderDetailVo) {
        Integer orderId=mallOrderDetailVo.getOrderId();
        Integer tradeNum=mallOrderDetailVo.getTradeNum();
        String invoiceType=mallOrderDetailVo.getInvoiceType();
        String  invoiceTitle=mallOrderDetailVo.getInvoiceTitle();
        String  invoiceDetail=mallOrderDetailVo.getInvoiceDetail();
        String  memo=mallOrderDetailVo.getMemo();
        if(CommonTools.isNullOrEmpty(orderId)||CommonTools.isNullOrEmpty(tradeNum)){
            throw new MallApiException(ExceptionCode.BASE_REQUEST_PARAM_ISNULL);//参数为空
        }
        MallOrderDetail orderDetail=mallOrderDetailMapper.getMallOrderDetailByID(orderId);
        if(orderDetail==null){
            throw new MallApiException(ExceptionCode.MALL_GOODS_ORDER_NULL);//参数为空
        }
        if(!CommonTools.isNullOrEmpty(addressId)){
            MallConsigneeAddress mallConsigneeAddress= mallConsigneeAddressMapper.getMallConsigneeAddressById(addressId);
            if(mallConsigneeAddress==null){
                throw new MallApiException(ExceptionCode.MALL_GOODS_ADDRESS_NULL);
            }else{
                //添加地址信息
                orderDetail.setAddress(mallConsigneeAddress.getAddress());
                orderDetail.setReceivePostcode(mallConsigneeAddress.getPostNum());
                orderDetail.setReceiver(mallConsigneeAddress.getConsignee());
                orderDetail.setReceiverMobile(mallConsigneeAddress.getConsigneeMobile());
            }
        }
        //从新计算金额
       BigDecimal price= orderDetail.getPrice();
       BigDecimal totalPrice=price.multiply(new BigDecimal(tradeNum));
        //todo 计算运费
        BigDecimal carriage=new BigDecimal(10);
       orderDetail.setTotalPrice(totalPrice.add(carriage));
        orderDetail.setCarriage(carriage);
       //计算总重量
       Double weight= orderDetail.getWeight();
       orderDetail.setTotalWeight(weight*tradeNum);
       if(CommonTools.isNullOrEmpty(invoiceType)||CommonTools.isNullOrEmpty(invoiceTitle)&&CommonTools.isNullOrEmpty(invoiceDetail)){
           //保存发票信息
           orderDetail.setInvoiceType(invoiceType);
           orderDetail.setInvoiceTitle(invoiceTitle);
           orderDetail.setInvoiceDetail(invoiceDetail);
           orderDetail.setInvoiceMoney(totalPrice);//开票金额为商品总价格（不含运费）
       }
        orderDetail.setMemo(memo);//备注信息
        orderDetail.setOrderStatus("1");//待付款状态
        mallOrderDetailMapper.updateMallOrderDetailByTemplate(orderDetail);
        return orderDetail;
    }
}
